package com.ysdz.tibetan.tibetanmini.product.service.impl;

import com.alibaba.fastjson.JSON;
import com.ysdz.tibetan.tibetanmini.product.beans.CategoryDetail;
import com.ysdz.tibetan.tibetanmini.product.beans.CategoryVO;
import com.ysdz.tibetan.tibetanmini.product.dao.CategoryDetailMapper;
import com.ysdz.tibetan.tibetanmini.utils.ResponseUtils;
import com.ysdz.tibetan.tibetanminicommons.common.ReturnMsg;
import com.ysdz.tibetan.tibetanmini.product.beans.Category;
import com.ysdz.tibetan.tibetanmini.product.dao.CategoryMapper;
import com.ysdz.tibetan.tibetanmini.product.service.CategoryService;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.ServletResponse;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.TimeUnit;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private CategoryDetailMapper categoryDetailMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private ServletResponse servletResponse;

    @Override
    public ReturnMsg getAllParentCategory() {
        String key = "category_parent";
        ValueOperations<String, List<Category>> operations = redisTemplate.opsForValue();
        boolean hasKey = redisTemplate.hasKey(key);
        if(hasKey){
            List<Category> categories = operations.get(key);
            return new ReturnMsg(ReturnMsg.SUCCESS_CODE, "成功！", categories);
        }else{
            // 从数据库中查询数据
            List<Category> categories = categoryMapper.selectAll();

            // 写入 Redis 缓存
            operations.set(key, categories, 5L, TimeUnit.MINUTES);
            return new ReturnMsg(ReturnMsg.SUCCESS_CODE, "成功！", categories);
        }
    }

    @Override
    public void getAllCategory() {
        String key = "category_all";
        ValueOperations<String, List<CategoryVO>> operations = redisTemplate.opsForValue();
        boolean hasKey = redisTemplate.hasKey(key);
        if(hasKey){
            List<CategoryVO> categories = operations.get(key);
//            return new ReturnMsg(ReturnMsg.SUCCESS_CODE, "成功！", categories);
            ResponseUtils.responseJson(servletResponse,new ReturnMsg(ReturnMsg.SUCCESS_CODE, "成功！", categories));
        }else{
            // 从数据库中查询数据
            List<Category> categories = categoryMapper.selectAll();
            List<CategoryVO> categoryVOS = new ArrayList<>();
            for (Category c : categories){
                // 根据父 ID 查出二级分类
                Example example = new Example(CategoryDetail.class);
                Example.Criteria criteria = example.createCriteria();
                criteria.andEqualTo("parentId", c.getCategoryId());
                List<CategoryDetail> categoryList = categoryDetailMapper.selectByExample(example);
                // 封装数据
                CategoryVO categoryVO = new CategoryVO(c);
                categoryVO.setChildren(categoryList);
                categoryVOS.add(categoryVO);
            }
            // 写入缓存
            operations.set(key, categoryVOS, 5L, TimeUnit.MINUTES);
//            return new ReturnMsg(ReturnMsg.SUCCESS_CODE, "成功！", categoryVOS);
            ResponseUtils.responseJson(servletResponse,new ReturnMsg(ReturnMsg.SUCCESS_CODE, "成功！", categoryVOS));
        }
    }
}
